using System;
using System.Collections.Generic;
using System.Linq;

namespace RStein.Posterous.API
{
    internal class SynchronisedSequenceGenerator
    {
        #region private variables
        private object m_lockRoot = new Object();
        private int m_start;
        private int m_end;
        private int m_current;

        #endregion private variables
        #region constructors
        public SynchronisedSequenceGenerator(int start, int end)
        {
            if (start > end)
            {
                throw new ArgumentException();
            }
            m_current = m_start = start;
            m_end = end;            
        }
        #endregion constructors
        public int GetNextNumber()
        {
            lock(m_lockRoot)
            {
                return getNumber();
            }
        }

        private int getNumber()
        {
            if (m_current == m_end)
            {
                m_current = m_start;
            }
            return ++m_current;
        }
    }
}